""" 
按键 步进电机控制开关
按下运行电机
松开停止电机

危险提示:
    暂停程序之后, 确认电机可能过热损坏

"""

#导入Pin模块
from machine import Pin
import time

# 按键引脚定义
key_start = Pin(14, Pin.IN, Pin.PULL_UP)

# 步进电机引脚定义
motor_a = Pin(15, Pin.OUT, Pin.PULL_DOWN)
motor_b = Pin(2, Pin.OUT, Pin.PULL_DOWN)
motor_c = Pin(0, Pin.OUT, Pin.PULL_DOWN)
motor_d = Pin(4, Pin.OUT, Pin.PULL_DOWN)

def setup_motor_send_pulse(setup):
    if setup == 1:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 2:
        motor_a.value(1)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 3:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 4:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(1)
        motor_d.value(0)
    elif setup == 5:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(0)
    elif setup == 6:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(1)
    elif setup == 7:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
    elif setup == 8:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)

if __name__ == "__main__":
    speed = 1
    setup = 1
    while True:
        if key_start.value() == 0:
            setup_motor_send_pulse(setup)
            time.sleep_ms(speed)
            setup += 1
            if setup > 8:
                setup = 1
        else:
            motor_a.value(0)
            motor_b.value(0)
            motor_c.value(0)
            motor_d.value(0)
            time.sleep_ms(speed)